package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.DailyContent;
import cn.wolfcode.trip.base.domain.DailyDetail;
import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.mapper.DailyContentMapper;
import cn.wolfcode.trip.base.mapper.DailyDetailMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IDailyDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DailyDetailServiceImpl implements IDailyDetailService {
    @Autowired
    private DailyDetailMapper dailyDetailMapper;
    @Autowired
    private DailyContentMapper contentMapper;

    public DailyDetail get(Long id) {
        return dailyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public DailyDetail listCommends(Integer state) {
        return dailyDetailMapper.selectByState(state);
    }

    @Override
    public List<DailyDetail> getCommend(Integer state) {
        return dailyDetailMapper.getCommend(state);
    }

    @Override
    public List<DailyDetail> getelse(Integer state) {
        return dailyDetailMapper.getCommend(state);
    }


    public DailyContent getContent(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    public void saveOrUpdate(DailyDetail entity) {
        Date d = new Date();
        //设置发布时间
        if (entity.getState() == DailyDetail.STATE_RECOMMEND) {
            entity.setReleaseTime(d);
        }
        //设置序列
        if (entity.getSequence() == null) {
            Integer sequence = dailyDetailMapper.selectMaxSequence(entity.getCatalog().getId());
            entity.setSequence(sequence != null ? sequence + 1 : 1);
        }
        DailyContent content = entity.getDailyContent();
        if (entity.getId() == null) {
            //处理当前是保存推荐,删除之前的头条推荐
            if (entity.getState() == DailyDetail.STATE_HEADLINE) {
                dailyDetailMapper.deleteOld();
            }
            //处理创建时间
            entity.setCreateTime(d);
            dailyDetailMapper.insert(entity);
            //设置主键关联的id
            content.setId(entity.getId());
            contentMapper.insert(content);
        } else {
            //当前不是保存推荐,并且要改为热门推荐
            if (checkOperation(entity)) {
                dailyDetailMapper.deleteOld();
            }
            entity.setReleaseTime(d);
            dailyDetailMapper.updateByPrimaryKey(entity);
            content.setId(entity.getId());
            contentMapper.updateByPrimaryKey(content);

        }
    }

    public PageInfo<DailyDetail> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<DailyDetail> list = dailyDetailMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    ///检查当前不是保存推荐,并且要改为热门推荐操作
    private boolean checkOperation(DailyDetail entity) {
        return entity.getState() == DailyDetail.STATE_HEADLINE &&
                dailyDetailMapper.selectTypeById(entity.getId()) != DailyDetail.STATE_HEADLINE;
    }
}
